'''
The module contains the functionalities that set options for
simulation process.

@author: Hatem Oraby
'''
import time
import socket

from overrideManager import OverrideManager


class Configuration:
    '''
    Provide emulation options (not complete).
    '''
    timeInstalled = False
    socketInstalled = False
    
    
class Network:
    '''
    Provides functionalities to set configuration parameters for
    simulated network.
    
    fixedSendDelay: bool: True value (default) imply to use the same
        sending delay (non-randomized) for all sent packets.
    
    SendDelays: dict: The send delays values to be used in sending. 
        The dict keys are the socket.family (e.g. socket.SOCK_STREAM),
        the value for each key is a tuple of two: (send delay,
        standard deviation). If _fixedSendDelay_ is True then only
        send delay value is used. If _fixedSendDelay_ is False then
        send delay along with standard deviation are used in a 
        Gaussian random function to generate the sending delay value.
    '''
    fixedSendDelay = True
    
    SendDelays = {  
                  socket.SOCK_DGRAM    : (0.15,0.03), #Putting any arbitrary values
                  socket.SOCK_STREAM   : (0.3,0.05),
                  '127.0.0.1': (0.000155,0.00001)
                  }
    
    def __init__(self):
        _time = OverrideManager.getEntity("time.time")  
        if _time:
            self._seedSendDelay = _time()
        else:
            self._seedSendDelay = time.time()
        
        if _time:
            self._seedPortGeneration = _time() + 1
        else:
            self._seedPortGeneration = time.time() + 1
    
    def seedSendDelay(self, seedValue):
        ''' 
        Seed the random delay generator with a new value, by default
        the random generator uses the current real time  as a seed for
        send delay generation.
        
        @type seedValue: C{float}
        @param seedValue: The value to seed the random send delay 
            generator with.
        '''
        from twistedSim.override import _socket
        _socket.sendDelayRndmProfile.seed(seedValue)

    
    def seedPortGeneration(self, seedValue):
        '''
        The port number assigned to a port is randomly generated. Use
        this method to seed the random port generator with a new
        value, by default the random seed generator uses the current
        real time  as a seed for port number generation.
        
        @type seedValue: C{float}
        @param seedValue: The value to seed the random port number 
            generator with.
        '''
        from twistedSim.override import _socket
        _socket.portGenerationRndmProfile.seed(seedValue)        
Network = Network()